﻿namespace GenericPattern.Sample
{
    using System;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Linq.Expressions;

    public class SampleContext : GuidDbContext, IDbContext<int>
    {
        public IDbSet<Foo> Foos { get; set; }
        public IDbSet<Bar> Bars { get; set; }

        IQueryable<TEntity> IDbContext<int>.All<TEntity>(params Expression<Func<TEntity, object>>[] includeProperties)
        {
            return includeProperties.Aggregate<Expression<Func<TEntity, object>>, IQueryable<TEntity>>(Set<TEntity>(), (current, includeProperty) => current.Include(includeProperty));
        }

        TEntity IDbContext<int>.Find<TEntity>(int id)
        {
            return Set<TEntity>().Find(id);
        }

        TEntity IDbContext<int>.Create<TEntity>()
        {
            return Set<TEntity>().Create();
        }

        int IDbContext<int>.Insert<TEntity>(TEntity entity)
        {
            Set<TEntity>().Add(entity);
            return entity.Id;
        }

        void IDbContext<int>.Update<TEntity>(TEntity entity)
        {
            Entry(entity).State = EntityState.Modified;
        }

        void IDbContext<int>.Delete<TEntity>(int id)
        {
            Set<TEntity>().Remove(Set<TEntity>().Find(id));
        }
    }
}
